package com.gopay.checkorder.scheduler;

import org.quartz.JobDetail;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.scheduling.quartz.CronTriggerBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;



/**
 * @GopaySchedulerFactory.java
 * @author fanghw
 * @2015年10月8日 上午10:34:44  www.gopay.com.cn Inc.All rights reserved.
 */
public class GopaySchedulerFactory extends SchedulerFactoryBean implements ApplicationContextAware {
    private final static Logger logger = LoggerFactory.getLogger(GopaySchedulerFactory.class);
    
    private ApplicationContext applicationContext;

    @Resource(name = "serverTriggerMap")
    private Map<String,List<CronTriggerBean>> serverTriggerMap;
    
    private boolean switchBCDL;
    private boolean switchICBC;
    private boolean switchBOC;
    private boolean switchCCB;
    private boolean switchABC;
    private boolean switchSCAN;
    private boolean switchOTHERBANK;
    private boolean switchMISC;
    private boolean switchNUCC;

    
    public void init() throws SchedulerException{
        logger.error("------------初始化触发器列表 start-------------");
        //获取触发器列表
        List<Trigger> triggerList=new ArrayList<Trigger>();
        if(switchBCDL){
            triggerList.addAll(serverTriggerMap.get("SERVER_BCDL"));
            logger.error("--switchBCDL="+switchBCDL);
        }
        if(switchICBC){
            triggerList.addAll(serverTriggerMap.get("SERVER_ICBC"));
            logger.error("--switchICBC="+switchICBC);
        }
        if(switchBOC){
            triggerList.addAll(serverTriggerMap.get("SERVER_BOC"));
            logger.error("--switchBOC="+switchBOC);
        }
        if(switchCCB){
            triggerList.addAll(serverTriggerMap.get("SERVER_CCB"));
            logger.error("--switchCCB="+switchCCB);
        }
        if(switchABC){
            triggerList.addAll(serverTriggerMap.get("SERVER_ABC"));
            logger.error("--switchABC="+switchABC);
        }
        if(switchSCAN){
            triggerList.addAll(serverTriggerMap.get("SERVER_SCAN"));
            logger.error("--switchSCAN="+switchSCAN);
        }
        if(switchOTHERBANK){
            triggerList.addAll(serverTriggerMap.get("SERVER_OTHERBANK"));
            logger.error("--switchOTHERBANK="+switchOTHERBANK);
        }
        if(switchMISC){
            triggerList.addAll(serverTriggerMap.get("SERVER_MISC"));
            logger.error("--switchMISC="+switchMISC);
        }
        if(switchNUCC){
            triggerList.addAll(serverTriggerMap.get("SERVER_NUCC"));
            logger.error("--switchNUCC="+switchNUCC);
        }
       
        
        logger.error("-------------triggerArr.length="+triggerList.size());
        
        //设置要执行的触发器
        String jobDetailName;
        JobDetail jobDetail;
        for(Trigger trigger:triggerList){
            jobDetailName=trigger.getJobKey().getName();
            logger.error("********jobDetailName="+jobDetailName);
            jobDetail=(JobDetail)applicationContext.getBean(jobDetailName);
            this.getScheduler().scheduleJob(jobDetail, trigger);
        }
        
        
        //启动定时任务
        this.getScheduler().start();
        
        
        logger.error("------------定时任务启动成功-------------");
        
        logger.error("------------初始化触发器列表 end-------------");
    }

    public void setSwitchBCDL(boolean switchBCDL) {
        this.switchBCDL = switchBCDL;
    }

    public void setSwitchICBC(boolean switchICBC) {
        this.switchICBC = switchICBC;
    }

    public void setSwitchBOC(boolean switchBOC) {
        this.switchBOC = switchBOC;
    }

    public void setSwitchCCB(boolean switchCCB) {
        this.switchCCB = switchCCB;
    }

    public void setSwitchOTHERBANK(boolean switchOTHERBANK) {
        this.switchOTHERBANK = switchOTHERBANK;
    }

    public void setSwitchMISC(boolean switchMISC) {
        this.switchMISC = switchMISC;
    }

    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

  

    public void setSwitchABC(boolean switchABC) {
        this.switchABC = switchABC;
    }

	public void setSwitchSCAN(boolean switchSCAN) {
		this.switchSCAN = switchSCAN;
	}

    public void setSwitchNUCC(boolean switchNUCC) {
        this.switchNUCC = switchNUCC;
    }
}
